package com.simpleaccount.modify;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import com.simpleaccount.Common.MySpinnerAdapter;
import com.simpleaccount.Db.DbUtils;
import com.simpleaccount.Entity.Category;
import com.simpleaccount.Entity.Entry;
import com.simpleaccount.R;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 超 on 2017/7/15.
 */

public class ModifyActivity extends Activity {


    @Bind(R.id.tv_back)
    ImageView tvBack;
    @Bind(R.id.et_money)
    EditText etMoney;
    @Bind(R.id.spinner_modify)
    Spinner spinnerModify;
    @Bind(R.id.datepicker_modify)
    DatePicker datepickerModify;
    @Bind(R.id.et_msg)
    EditText etMsg;
    @Bind(R.id.tv_submit)
    TextView tvSubmit;

    private int year;
    private int month;
    private int day;
    private double number;
    private String remark;
    private int year_new;
    private int month_new;
    private int day_new;
    private double number_new;
    private String category_new;
    private String remark_new;
    Entry entry;


    public List<String> getData() {
        List<String> list = new ArrayList<>();
        for (Category e:DbUtils.getCategory(1)) {
            list.add(e.getName());
        }
        return list;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify);
        ButterKnife.bind(this);
        entry = (Entry) getIntent().getSerializableExtra("entry");
        MySpinnerAdapter adapter = new MySpinnerAdapter(this, getData());
        spinnerModify.setAdapter(adapter);
        int position = 0;
        List list = getData();
        for (int i = 0; i < getData().size(); i++) {
            if (entry.getCategory().equals(list.get(i))) {
                position = i;
                break;
            }

        }
//
        year = entry.getDate().getYear() + 1900;
        month = entry.getDate().getMonth();
        day = entry.getDate().getDate();
        number = entry.getMoney();
        remark = entry.getMsg();

        etMoney.setText(String.format("%.2f", number));
        spinnerModify.setSelection(position, true);

        datepickerModify.init(year, month, day, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker arg0, int year, int month, int day) {
                ModifyActivity.this.year = year;
                ModifyActivity.this.month = month;
                ModifyActivity.this.day = day;

            }
        });
        etMsg.setText(remark);
    }

    public void onClickModify() {
        number_new = Double.valueOf(etMoney.getText().toString());
        category_new = spinnerModify.getSelectedItem().toString();
        year_new = datepickerModify.getYear();
        month_new = datepickerModify.getMonth();
        day_new = datepickerModify.getDayOfMonth();
        remark_new = etMsg.getText().toString();
        Date date_new = new Date(year_new - 1900, month_new, day_new);
        Entry entry_new = new Entry(entry.getId(), number_new, DbUtils.getCategory(category_new), date_new, remark_new);
        DbUtils.modifyEntry(entry_new);
        Intent intent = getIntent();
        setResult(0, intent);
        finish();
    }

    @OnClick({R.id.tv_back, R.id.tv_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_back:
                Intent intent = getIntent();
                setResult(-1, intent);
                finish();
                break;
            case R.id.tv_submit:
                onClickModify();
                break;
        }
    }

    public void onBackPressed() {
        Intent intent = getIntent();
        setResult(-1, intent);
        finish();
    }
}
